      var Plant = Ext.data.Record.create([
           // the "name" below matches the tag name to read, except "availDate"
           // which is mapped to the tag "availability"
            {name: 'id'},
           {name: 'unit', type: 'string'},
           {name: 'quantity', type: 'float'},
           {name: 'herbName', type: 'string'}
      ]);
     var viewBean = Ext.data.Record.create([
           // the "name" below matches the tag name to read, except "availDate"
           // which is mapped to the tag "availability"
            {name: 'id'},
           {name: 'name', type: 'string'}
      ]);
        // shorthand alias
    var fm = Ext.form;
    
    //////////////////////折算剂量结果////////////////////////////////////////
    var convertCom =  [{id:'AA',header: "药名", width: 40,sortable: true, dataIndex: 'herbName', hidden:false},
            {header: "原剂量", width: 50, sortable: true, dataIndex: 'dosage'}];
    var convertFields = ['herbName', 'dosage'];
	 var storeConvert = new Ext.data.JsonStore({
        root: 'articles',
        fields: convertFields,
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: basePath+'/../prescription',
            doClass:'convert'
        }),
        baseParams: {doClass:'convert'}
    });
    storeConvert.setDefaultSort('id', 'desc');


    // create the Grid
    var gridConvert = new Ext.grid.GridPanel({
        store: storeConvert,
        shadow:true,
        autoScroll: true,
        title:'折算剂量',
        border:false,
        frame:true,
        columns: convertCom,
         iconCls:'icon-grid',
         bodyStyle:'width:100%;',
		 autoWidth:true,
         height:500,
         sm: new Ext.grid.CheckboxSelectionModel({singleSelect: false})
    });
    
     gridConvert.render('gridConvert');
      ////////////////////折算规则/////////////////////////////////////////////////////
  ///////////////////////////////
     var storeRulePrescription = new Ext.data.JsonStore({
        root: 'articles',
        fields: [
            'id', 'unit','quantity','herbName'
        ],
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: basePath+'/../viewServlet',
            doClass:'viewRules'
        }),
        baseParams: {doClass:'viewRules'}
    });
    storeRulePrescription.setDefaultSort('id', 'desc');

    // create the Grid
    var gridRulePrescription = new Ext.grid.EditorGridPanel({
        store: storeRulePrescription,
        shadow:true,
        autoScroll: true,
        border:false,
         frame:true,
        columns: [
       		 new Ext.grid.CheckboxSelectionModel(),
            {id:'',header: "选择", width: 55,sortable: true, dataIndex: 'id', hidden:true},
            {header: "原始单位", width: 65, sortable: true, dataIndex: 'unit', editor: new fm.TextField({
	               allowBlank: false
	           })},
             {header: "折算比例", width: 65, sortable: true, dataIndex: 'quantity', editor: new fm.NumberField({
	               allowBlank: false
	           })},
               {header: "中药名称", width: 65, sortable: true, dataIndex: 'herbName', editor: new fm.TextField({
	               allowBlank: false
	           })}
        ],
         iconCls:'icon-grid',
        title:'计量折算规则区',
         width:250,
         height:300,
          clicksToEdit:2,
        tbar: [{
            text: '添加',
            handler : function(){
               var  p =new Plant({
           		unit:'枚',
           		quantity:'1',
                herbName:''
               });
                gridRulePrescription.stopEditing();
              	storeRulePrescription.insert(storeRulePrescription.getCount(), p);
                gridRulePrescription.startEditing(0, 0);
            }
        },{
            text: '删除',
            handler : function(){
            	 var o = gridRulePrescription.getSelectionModel().getSelections();
            	 if(o.length == 0)
            	 	alert('选择规则');
            	 else 
            	 { 
        			Ext.MessageBox.confirm('提示', '删除确认,删除后不可恢复!', function(button,text){
	        			if(button == 'yes') {
	      					var storeIds = new Array();
		            	 	var ids = new Array();
		            	 	var size = o.length;
		            	 
		            	 	for(var i=0 ;i < size ;i++){
		            	 		ids.push(o[i].data.id);
		            	 		storeIds.push(o[i].id);
		            	 	}
		            	 	
		           	 	     Ext.Ajax.request({
							   url: basePath+'/../viewServlet',
							   success: successRulePrescriptionDel,
							   params: {
								   doClass:'ruleDel',
								   ids:ids,
								   storeIds:storeIds,
								    viewId:gridRulePrescription.viewId
							   }
							});
      					}
        			});
			
            	 }
            	 
            }
        }],
         sm: new Ext.grid.CheckboxSelectionModel({singleSelect: false})
    });
   
     gridRulePrescription.on('afteredit',function(e){
		     Ext.Ajax.request({
		   url: basePath+'/../viewServlet',
		   success: successRulePrescription,
		   params: {
			   doClass:'ruleEdit',
			   id:e.record.data.id,
			   unit:e.record.data.unit,
			   quantity:e.record.data.quantity,
			   herbName:e.record.data.herbName,
			   viewId:gridRulePrescription.viewId,
			   storeId:e.record.id
		   }
		});
		
		  gridRulePrescription.stopEditing();
    }); 
	
    gridRulePrescription.render('gridRulePrescription');


    // trigger the data store load
  ///////////////计量观点/////////////////////////////
   var storeView = new Ext.data.JsonStore({
        root: 'articles',
        fields: [
            'id', 'name'
        ],
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: basePath+'/../viewServlet',
            doClass:'view'
        }),
        baseParams: {doClass:'view'}
    });
    storeView.setDefaultSort('id', 'desc');


    // create the Grid
    var gridView = new Ext.grid.EditorGridPanel({
        store: storeView,
        shadow:true,
        autoScroll: true,
        width:250,
        height:200,
        border:false,
        frame:true,
        columns: [
        	new Ext.grid.CheckboxSelectionModel(),
            {id:'AA',header: "观点id", width: 90,sortable: true,  hidden:true,dataIndex: 'id'},
            {header: "观点", width: 90,sortable: true, dataIndex: 'name',
	           editor: new fm.TextField({
	               allowBlank: false
	           })}
        ],
         iconCls:'icon-grid',
         clicksToEdit:2,
            tbar: [{
            text: '添加观点',
            handler : function(){
                var v = new viewBean({id:'',name:'观点名'});
                gridView.stopEditing();
                storeView.insert(0, v);
                gridView.startEditing(0, 0);
            }
        }],
         sm: new Ext.grid.CheckboxSelectionModel({singleSelect: false})
    });
    gridView.render('gridView');
    gridView.on('afteredit',function(e){
     		gridView.stopEditing();
		    Ext.Ajax.request({
		   url: basePath+'/../viewServlet',
		   success: successFn,
		   failure: failureFn,
		   params: {
		   doClass:'viewEdit',
		   id:e.record.data.id,
		   name:e.record.data.name,
		   storeId:e.record.id
		   }
		});
    }); 
    gridView.on('rowclick', function(g, rowIdx, e) {
		 var o = g.getSelectionModel().getSelections();
		 gridRulePrescription.setTitle(o[o.length-1].data.name);
		 var size  = o.length;
		 ///////////////重新加载折算数据////////////////////////
		 var comArray = new Array();
		  var comArrayFields = new Array();
		  var convertViewId =  new Array();
		 for(var i=0 ; i<size ;i++){
		 	comArray.push({header: o[i].data.name+'剂量', width: 80, sortable: true, dataIndex: 'id_'+o[i].data.id+'_'});
		 	comArrayFields.push('id_'+o[i].data.id+'_');
		 	convertViewId.push(o[i].data.id);
		 }
		  gridConvert.reconfigure(new Ext.data.JsonStore({
		        root: 'articles',
		        fields: convertFields.concat(comArrayFields),
		        proxy: new Ext.data.HttpProxy({
		            url: basePath+'/../prescription',
		            doClass:'convert'
		        }),
		        baseParams: {doClass:'convert'}
		    	}),new Ext.grid.ColumnModel(convertCom.concat(comArray)));
		gridConvert.getStore().removeAll();
		gridConvert.getStore().load({params:{doClass:'convert',viewId:convertViewId,fjId:fjIdCovert}});
		//////////////////////////////////////////////////
		 storeRulePrescription.removeAll();
		 if(o[o.length-1].data.id == '')
		 ;
		 else
		{
		  gridRulePrescription.viewId = o[o.length-1].data.id;
		  storeRulePrescription.load({params:{doClass:'viewRules',viewId:o[o.length-1].data.id}});
		  
		  } 
	
      ////////////////////////////////////
	});

   
       storeView.load({params:{doClass:'view'}});
       ///////添加成功后 刷新 record
	function successFn(v){
		var json = Ext.decode(v.responseText);
		if(json.successFn){
			var o = storeView.getById(json.storeId);
			if(o){
				o.data.id = json.vId;
				gridRulePrescription.viewId = json.vId;
				 storeRulePrescription.load({params:{doClass:'viewRules',viewId:json.vId}});
				o.commit(false);
			}
		}else{
		 	alert('更新失败');
		}
		gridView.startEditing(0, 0);
	}
	function failureFn(){
	}
	function successRulePrescription(v){
		var json = Ext.decode(v.responseText);
		if(json.successFn){
			var o = storeRulePrescription.getById(json.storeId);
			if(o){
				o.data.id = json.rId;
				o.commit(false);
			}
		}else{
		 	alert('更新失败');
		}
		gridRulePrescription.startEditing(0, 0);
	}
	function successRulePrescriptionDel(v){
		var json = Ext.decode(v.responseText);
		if(json.successFn){
		  var size = json.storeId.length;
		  for(var i=0 ; i<size ;i++){
			  var o = storeRulePrescription.getById(json.storeId[i]);
			  storeRulePrescription.remove(o);
		  }
		}else{
		 	alert('删除失败');
		}
		gridRulePrescription.startEditing(0, 0);
	}